<?php

namespace App\Sms;

use App\Sms\Contracts\Sms;
use Log;

include app_path('Libs/TopSms/TopSdk.php');

class TopSms implements Sms
{

    public function sendTmplSms($phone,$tmpl,$data,$extend=null)
    {
        try {
            $sms_conf = config('sms');
            $conf = $sms_conf['gateways']['TopSms'];
            $key = $conf['key'];
            $secret = $conf['secret'];
            $sign = $conf['sign'];



            $c = new \TopClient;
            $c->appkey = $key;
            $c->secretKey = $secret;
            $c->format = 'json';
            $req = new \AlibabaAliqinFcSmsNumSendRequest;
            $req->setExtend($extend);

            $req->setSmsType("normal");
            $req->setSmsFreeSignName($sign);
            $req->setSmsParam(json_encode($data));
            $req->setRecNum($phone);
            $req->setSmsTemplateCode($tmpl);
            $json = $c->execute($req);



            if(!property_exists($json,'result')  ) {
                Log::error("发送短信失败：".var_export($json,true));
                return false;
            } else if( property_exists($json,'result') && property_exists($json->result,'success')  ) {
                return true;
            } else {
                Log::error("发送短信失败：".var_export($json,true));
            }

            return true;


        }catch (\Exception $e) {
            Log::error("发送短信失败：".$e->getMessage());
            return false;
        }




    }
}
